package com.cheer.ba.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.cheer.ba.R;
import com.cheer.ba.model.BannerModel;
import com.cheer.ba.utils.DensityUtils;
import com.cheer.ba.utils.Protect;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.view.activity.CommH5RefreshAty;
import com.cheer.ba.view.activity.CommH5WithTitleAty;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * banner适配器
 */
public class BannerPagerAdapter extends PagerAdapter {

    private final Random random = new Random();

    private List<BannerModel> mBanners = new ArrayList<BannerModel>();

    private Context mContext;

    public BannerPagerAdapter(List<BannerModel> mBanners, Context mContext) {
        this.mContext = mContext;
        if (null != mBanners && !mBanners.isEmpty()) {
            this.mBanners.clear();
            this.mBanners.addAll(mBanners);
        }
    }

    @Override
    public int getCount() {
        return mBanners.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup view, int position, Object object) {
        view.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup view, int position) {
        return getView(view, position);
    }

    private ImageView getView(ViewGroup view, int pos) {
        final BannerModel banner = mBanners.get(pos);
        ImageView iv = new ImageView(view.getContext());
        iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
        iv.setImageDrawable(mContext.getResources().getDrawable(R.color.color_F5F5F5));
        view.addView(iv, ViewGroup.LayoutParams.MATCH_PARENT, DensityUtils.getScreenW(mContext) * 344 / 750);

        final String picPath = banner.getBanner();
        if (!TextUtils.isEmpty(picPath)) {
            if (picPath.endsWith(".gif")) {
                if (Protect.checkLoadImageStatus(mContext)) {
                    Glide.with(mContext).load(Protect.setPic(picPath)).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(R.drawable.ic_banner_load).error(R.drawable.ic_banner_load).into(iv);
                }
            } else {
                if (Protect.checkLoadImageStatus(mContext)) {
                    Glide.with(mContext).load(Protect.setPic(picPath)).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.drawable.ic_banner_load).error(R.drawable.ic_banner_load).into(iv);
                }
            }
        } else {
            iv.setImageResource(R.drawable.ic_banner_load);
        }
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url = banner.getUrl();
                if (!StringUtils.isEmpty(url) && url.contains("http")) {
//                    if (banner.mediaType == 0) {
//                        //视频
//                        Intent intent = new Intent(mContext, FullScreenAty.class);
//                        intent.putExtra(IntentKey.General.KEY_URL, url);
////                    intent.putExtra(IntentKey.KEY_PATH, "http://oery7dqsp.bkt.clouddn.com/yryz-720p.mp4");
//                        intent.putExtra(IntentKey.General.KEY_BOOLEAN, false);
//                        mContext.startActivity(intent);
//                    } else if (banner.mediaType == 1) {
                    CommH5RefreshAty.startCommH5RefreshAty((Activity) view.getContext(), banner.getUrl(), "banner");
//                    } else if (banner.getMediaType() == 2) {
//                        //不做任何处理
//
//                    }
                } else {
//                    if (!StringUtils.isEmpty(picPath)) {
//                        SVProgressHUD.showInfoWithStatus(mContext, "路径错误");
//                    }
                }
            }
        });
        return iv;
    }
}